package com.yytek.am.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yytek.am.domain.FmEventHis;
import com.yytek.am.mapper.FmEventHisMapper;
import com.yytek.common.core.utils.DateUtils;
import com.yytek.common.core.utils.bean.BeanCopyUtils;
import com.yytek.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yytek.am.mapper.FmEventMapper;
import com.yytek.am.domain.FmEvent;
import com.yytek.am.service.IFmEventService;

/**
 * 告警事件Service业务层处理
 * 
 * @author yytek-iot
 * @date 2022-10-10
 */
@Service
public class FmEventServiceImpl implements IFmEventService 
{
    @Autowired
    private FmEventMapper fmEventMapper;
    @Autowired
    private FmEventHisMapper fmEventHisMapper;
    /**
     * 查询告警事件
     * 
     * @param id 告警事件主键
     * @return 告警事件
     */
    @Override
    public FmEvent selectFmEventById(Long id)
    {
        return fmEventMapper.selectFmEventById(id);
    }

    /**
     * 查询告警事件列表
     * 
     * @param fmEvent 告警事件
     * @return 告警事件
     */
    @Override
    public List<FmEvent> selectFmEventList(FmEvent fmEvent)
    {
        return fmEventMapper.selectFmEventList(fmEvent);
    }

    /**
     * 新增告警事件
     * 
     * @param fmEvent 告警事件
     * @return 结果
     */
    @Override
    public int insertFmEvent(FmEvent fmEvent)
    {
        fmEvent.setCreateTime(DateUtils.getNowDate());
        return fmEventMapper.insertFmEvent(fmEvent);
    }

    /**
     * 修改告警事件
     * 
     * @param fmEvent 告警事件
     * @return 结果
     */
    @Override
    public int updateFmEvent(FmEvent fmEvent)
    {
        fmEvent.setUpdateTime(DateUtils.getNowDate());
        return fmEventMapper.updateFmEvent(fmEvent);
    }

    /**
     * 批量删除告警事件
     * 
     * @param ids 需要删除的告警事件主键
     * @return 结果
     */
    @Override
    public int deleteFmEventByIds(Long[] ids)
    {
        return fmEventMapper.deleteFmEventByIds(ids);
    }

    /**
     * 删除告警事件信息
     * 
     * @param id 告警事件主键
     * @return 结果
     */
    @Override
    public int deleteFmEventById(Long id)
    {
        return fmEventMapper.deleteFmEventById(id);
    }

    /**
     * 活动告警清除
     * @param id
     * @return
     */
    @Override
    public int clearEvent(String id,String note) {
        //查询活动告警
        FmEvent fmEvent = fmEventMapper.selectFmEventById(Long.valueOf(id));
        if(fmEvent != null){
            //复制活动告警到历史告警
            FmEventHis fmEventHis = BeanCopyUtils.copy(fmEvent, FmEventHis.class);
            fmEventHis.setId(null);
            //未确认状态
            fmEventHis.setAckFlag(0);
            fmEventHis.setAckNote(null);
            fmEventHis.setAckOperator(null);
            fmEventHis.setAckTime(null);
            //设置清除时间和清除人
            fmEventHis.setClearTime(new Date());
            String userId = SecurityUtils.getUserId().toString();
            fmEventHis.setClearOperator(userId);
            fmEventHis.setClearNote(note);
            fmEventHisMapper.insertFmEventHis(fmEventHis);
            //成功返回1,失败返回0
            return 1;
        }else{
            return 0;
        }

    }

    /**
     * 获取各种告警类型数量
     * @param fmEvent
     * @return
     */
    @Override
    public Map<String, Object> selectAllTypeNum(FmEvent fmEvent) {
        return fmEventMapper.selectAllTypeNum(fmEvent);
    }
}
